package com.xuecheng.teaching.controller;

import com.xuecheng.api.content.model.dto.CoursePubDTO;
import com.xuecheng.api.teaching.WorkRecordApi;
import com.xuecheng.api.teaching.model.dto.CourseWorkRecDTO;
import com.xuecheng.api.teaching.model.dto.WorkDTO;
import com.xuecheng.api.teaching.model.dto.WorkRecordDTO;
import com.xuecheng.api.teaching.model.qo.QueryWorkModel;
import com.xuecheng.api.teaching.model.vo.WorkRecordVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.uaa.LoginUser;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.teaching.common.constant.TeachingErrorCode;
import com.xuecheng.teaching.common.util.UAASecurityUtil;
import com.xuecheng.teaching.convert.WordRecordConvert;
import com.xuecheng.teaching.convert.WorkRecordConvert;
import com.xuecheng.teaching.service.CourseWorkRecService;
import com.xuecheng.teaching.service.WorkRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 作业提交记录 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class WorkRecordController implements WorkRecordApi {

    @Autowired
    private WorkRecordService workRecordService;

    @Autowired
    private CourseWorkRecService courseWorkRecService;

    /*
     *新增或修改作业记录
     */
    @PostMapping("work-record")
    public WorkRecordDTO createOrWorkRecord(@RequestBody WorkRecordVO workRecordVo) {
        // 通过工具类获取学生信息（页面请求的视图数据里没有ID值）
        LoginUser user = UAASecurityUtil.getUser();
        String username = user.getUsername();
        //将vo转换为dto
        WorkRecordDTO workRecordDTO = WordRecordConvert.INSTANCE.vo2dto(workRecordVo);
        //调用WorkService层新增或修改作业方法
        WorkRecordDTO result = workRecordService.createOrWorkRecord(workRecordDTO, username);
        return result;
    }


    //作业批改-分页查询基本作业信息
    @PostMapping("/work-record/list")
    public PageVO queryWork(PageRequestParams params, @RequestBody QueryWorkModel model) {

        //获取公司id
        Long companyId = UAASecurityUtil.getCompanyId();

        //调service方法
        PageVO<CourseWorkRecDTO> pageVO = courseWorkRecService.queryWork(params, model,companyId);


        return pageVO;
    }

    /**
     * 批阅详情
     * @param courseWorkId 课程作业提交id
     * @return CourseWorkRecDTO
     */
    @Override
    @GetMapping("/work-record/read-over-all/{courseWorkId}")
    public CourseWorkRecDTO readOverAll(@PathVariable("courseWorkId") String courseWorkId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        if (ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(TeachingErrorCode.E_130506);
        }
        return workRecordService.readOverAll(companyId,courseWorkId);
    }




   /*
    *教师批改意见提交
    */
    @PutMapping("work-record/correction")
    public WorkRecordDTO submitWorkRecord(@RequestBody WorkRecordVO workRecordVO) {

        // 通过工具类获取机构ID（页面请求的视图数据里没有ID值）
        Long companyId = UAASecurityUtil.getCompanyId();

        //将vo转换为dto
        WorkRecordDTO workRecordDTO = WorkRecordConvert.INSTANCE.vo2dto(workRecordVO);

        //调用workRecordService方法
        WorkRecordDTO resultDTO =  workRecordService.submitWorkRecord(workRecordDTO,companyId);

        return resultDTO;
    }

    /*
     *根据workId查询课程计划作业填写请况
     */
    @GetMapping("work/{workId}")
    public WorkDTO queryWorkRecord(@PathVariable Long workId) {


        //通过工具类获取用户信息
        LoginUser user = UAASecurityUtil.getUser();
        //通过工具类获取用户名字
        String username = user.getUsername();

        //调用workRecordService方法
        WorkDTO resultDTO =  workRecordService.queryWorkRecord(workId,username);
        return resultDTO;
    }

    @PostMapping("/l/work-record")
    public void createCourseWorkRecByCourseId(@RequestBody CoursePubDTO dto, @RequestParam Integer count) {
        workRecordService.createCourseWorkRecByCourseId(dto,count);

    }
}
